package com.ysjweb.dish.Service;


import com.ysjweb.dish.Mapper.DishMapper;
import com.ysjweb.dish.Mapper.SuggestionMapper;
import com.ysjweb.dish.Mapper.UserMapper;
import com.ysjweb.dish.Service.impl.DishServiceImpl;
import com.ysjweb.dish.Service.impl.GetWeather;
import com.ysjweb.dish.pojo.Couple;
import com.ysjweb.dish.pojo.WeatherFather;
import com.ysjweb.dish.pojo.dish;
import com.ysjweb.dish.pojo.WeatherHistorical;
import com.ysjweb.dish.Service.impl.AiConsult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Flux;

import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;

@Service
public class dishService implements DishServiceImpl {
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SuggestionMapper suggestionMapper;
    @Autowired
    private AiConsult aiConsult;
    @Autowired
    private GetWeather getWeather;
    @Autowired
    private UserMapper userMapper;

    @Override
    public String GetPicture(String dishName) {

        return dishMapper.GetPictureById(dishMapper.GetDishIdByName(dishName));
    }

    @Override
    public String GetVideo(String dishName) {
        
        return dishMapper.GetVideoById(dishMapper.GetDishIdByName(dishName));
    }

    @Override
    public String GetUserWeather(String site, LocalDate time){
        WeatherFather weatherData;
        int diff=(int) ChronoUnit.DAYS.between(LocalDate.now(),time);//time-now
        if(diff>0){
            weatherData=getWeather.getForecastWeather(site,diff);
        } else if (diff<0) {
            weatherData=getWeather.getHistoricalWeather(site,time);
        }else {
            weatherData=getWeather.getCurrentWeather(site);
        }
        //通过天气预报api获取天气情况
        //分为 实时 历史 预报 三类来汇报天气
        return "气温为"+weatherData.getTemperature()+"度,天气为"+weatherData.getText();//
    }

    @Override
    public String GetDishName(String weather,Integer id,String mealTime,String constitution, String site){


        //再将weather和时间地点还有用户信息作为参数 一起调用智能体的API得到菜品名称


        //将原本的做法和用户建议两个String加到一起给大模型得到菜品如何做


        String DishName="从";//
        ArrayList<String> menu=dishMapper.GetDishName();
        for(int i=0;i<menu.size();i++){
            DishName=DishName+menu.get(i)+",";
        }
        DishName=DishName+"中推荐最适合在"+weather+"下在"+site+"作为"+mealTime;
        String favorite=userMapper.GetFavoriteById(id);
        DishName+="且用户体质为"+constitution;
        if(favorite!=null){
            DishName+="、用户喜好为"+favorite+"时吃的食物(只返回菜名)";
        }
        return  aiConsult.RecommendDish(DishName);
    }

    @Override
    public Flux<String> RecommendDish(String DishName,String weather,Integer id,String mealTime,String constitution, String site) {



        String suggestion=suggestionMapper.GetSuggestion(id,dishMapper.GetDishIdByName(DishName));
        String need="说明在"+weather+"天气下在"+site+"作为"+mealTime+"且用户体质为"+constitution+"适合吃"+DishName+"的理由。并结合"+dishMapper.GetProcessByName(DishName);
        if(suggestion!=null){
            need=need+"和"+suggestion;;
        }
        need+="说明如何制作这道菜";
        Flux<String> recommend=aiConsult.consult(need);
        return recommend;
    }

    @Override
    public dish GetBase(Integer id) {
        dish base=dishMapper.GetDishById(id);
        return  base;
    }

    @Override
    public void UpdateAI(String suggestion,Integer id) {
        //将suggestion传给智能体
        
    }

    @Override
    public Couple AddCuisine(Integer UserId, dish dish) {
        dishMapper.AddDish(dish);
        //此处要改
        suggestionMapper.AddConnection(dishMapper.GetDishId(),UserId);
        Couple couple=new Couple();
        couple.setDishId(dishMapper.GetDishId());
        couple.setUserId(UserId);
        return couple;
    }

    @Override
    public String GetPictureById(Integer id) {
        return dishMapper.GetPictureById(id);
    }

}
